package com.cdn.log.aspect;

import com.cdn.log.consts.CLogConst;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * @author 蔡定努
 *  2024/06/03 14:45
 */
@Aspect
public class ScheduledAop {

    @Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void scheduledMethods() {
    }


    @Before("scheduledMethods()")
    public void addTraceId() {
        MDC.put(CLogConst.TRACE_ID, UUID.randomUUID().toString());
    }

    @After("scheduledMethods()")
    public void removeTraceId(JoinPoint joinPoint) {
        MDC.remove(CLogConst.TRACE_ID);
    }

    @AfterThrowing("scheduledMethods()")
    public void exRemoveId(JoinPoint joinPoint) {
        MDC.remove(CLogConst.TRACE_ID);
    }


}
